package com.wjk.kylin.modules.system.schedule.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wjk.kylin.core.common.aspect.Log;
import com.wjk.kylin.core.common.controller.BaseController;
import com.wjk.kylin.core.common.data.RequestData;
import com.wjk.kylin.core.common.data.ResponseData;
import static com.wjk.kylin.core.constant.CommonConstant.BusinessType.*;
import com.wjk.kylin.core.utils.PageUtils;
import com.wjk.kylin.modules.system.schedule.entity.ScheduleLog;
import com.wjk.kylin.modules.system.schedule.service.IScheduleLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  定时任务日志 前端控制器
 * </p>
 *
 * @author wangjkui
 * @since 2020-06-04
 */
@Api("定时任务日志")
@RestController
@RequestMapping("/sys/schedule/log")
public class ScheduleLogController extends BaseController {

    @Autowired
    private IScheduleLogService scheduleLogService;

    @PreAuthorize("hasAuthority('system:schedule:list')")
    @ApiOperation("列表")
    @Log(title = "定时任务日志-列表",businessType = LIST,isSaveResponseData = false)
    @GetMapping("/list")
    public ResponseData list(ScheduleLog scheduleLog) {
        IPage<ScheduleLog> page = scheduleLogService.findPage(new RequestData<ScheduleLog>(), scheduleLog);
        return ResponseData.success().data("page", new PageUtils(page));
    }

    @PreAuthorize("hasAuthority('system:schedule:query')")
    @ApiOperation("详情")
    @Log(title = "定时任务日志-详情",businessType = GET)
    @GetMapping("/{scheduleLogId}")
    public ResponseData get(@PathVariable String scheduleLogId) {
        ScheduleLog scheduleLog = scheduleLogService.getById(scheduleLogId);
        return ResponseData.success().data("scheduleLog", scheduleLog);
    }

    @PreAuthorize("hasAuthority('system:schedule:delete')")
    @ApiOperation("删除")
    @Log(title = "定时任务日志-删除",businessType = DELETE)
    @DeleteMapping("/{scheduleLogIds}")
    public ResponseData delete(@PathVariable String[] scheduleLogIds) {

        boolean flag = scheduleLogService.deleteByIds(scheduleLogIds);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("删除失败");
        }
    }

    @PreAuthorize("hasAuthority('system:schedule:delete')")
    @ApiOperation("清空")
    @Log(title = "定时任务日志-清空",businessType = CLEAN)
    @DeleteMapping("/clean")
    public ResponseData clean() {
        scheduleLogService.clean();
        return ResponseData.success();
    }

}
